package cn.walls1717.wallsmall.product.app;

import cn.walls1717.wallsmall.common.utils.PageUtils;
import cn.walls1717.wallsmall.common.utils.R;
import cn.walls1717.wallsmall.product.entity.AttrAttrgroupRelationEntity;
import cn.walls1717.wallsmall.product.entity.AttrEntity;
import cn.walls1717.wallsmall.product.entity.AttrGroupEntity;
import cn.walls1717.wallsmall.product.service.AttrAttrgroupRelationService;
import cn.walls1717.wallsmall.product.service.AttrGroupService;
import cn.walls1717.wallsmall.product.service.AttrService;
import cn.walls1717.wallsmall.product.service.CategoryService;
import cn.walls1717.wallsmall.product.vo.AttrGroupWithAttrsVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 属性分组
 *
 * @author walls1717
 * @email walls1717@163.com
 * @date 2022-10-21 11:58:42
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Resource
    private AttrGroupService attrGroupService;
    @Resource
    private CategoryService categoryService;
    @Resource
    private AttrService attrService;
    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 获取分类下所有分组&关联属性
     */
    @GetMapping("/{catalogId}/withattr")
    public R listAttrGroupsWithAttrs(@PathVariable("catalogId") Long catalogId) {
        List<AttrGroupWithAttrsVO> attrGroupWithAttrsVOS = attrGroupService.listAttrGroupsWithAttrs(catalogId);
        return R.ok().put("data", attrGroupWithAttrsVOS);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    // @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Integer catelogId) {
        // PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.listPages(params, catelogId);

        return R.ok().put("page", page);
    }

    /**
     * 获取属性分组的关联的所有属性
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R getAttrRelation(@PathVariable("attrgroupId") Long attrGroupId) {
        List<AttrEntity> attrs = attrService.listAttrsByAttrGroupId(attrGroupId);
        return R.ok().put("data", attrs);
    }

    /**
     * 获取属性分组没有关联的其他属性
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getNoAttrRelation(@RequestParam Map<String, Object> params,
        @PathVariable("attrgroupId") Long attrGroupId) {
        PageUtils page = attrService.listNoAttrsByAttrGroupId(params, attrGroupId);
        return R.ok().put("page", page);
    }

    /**
     * 添加属性与分组关联关系
     */
    @PostMapping("/attr/relation")
    public R saveAttrAttrGroupRelation(@RequestBody List<AttrAttrgroupRelationEntity> attrAttrgroupRelations) {
        attrAttrgroupRelationService.saveBatch(attrAttrgroupRelations);
        return R.ok();
    }

    /**
     * 删除属性与分组的关联关系
     */
    @PostMapping("/attr/relation/delete")
    public R removeAttrAttrGroupRelation(@RequestBody List<AttrAttrgroupRelationEntity> attrAttrgroupRelations) {
        attrAttrgroupRelationService.removeAttrAttrGroupRelationByAttrIdAndCatelogId(attrAttrgroupRelations);
        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    // @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] path = categoryService.getCatalogPath(catelogId);
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attrgroup:save");
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
